let myRequest = require('../../config/myRequest.js');
let api = require('../../config/api.js');
const app = getApp();

Component({
  /**
   * 组件的属性列表,定义组件内部要接受外部的值
   */
  properties: {
    goodsname: {
      type: String,
      value: ''
    },
    goodssort: {
      type: String,
      value: ''
    },
    goodscat_id1: {
      type: String,
      value: ''
    },
    goodscat_name1: {
      type: String,
      value: ''
    },
    goodscat_id2: {
      type: String,
      value: ''
    },
    goodscat_name2: {
      type: String,
      value: ''
    },
    goodsweight: {
      type: String,
      value: ''
    },
    prince: {
      type: String,
      value: ''
    },
    allnum: {
      type: String,
      value: ''
    },
    ids: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据 . 定义页面要传输给组件内部的值
   */
  data: {
    //弹窗显示控制
    showModalStatus: false,
    className: '',
    multiIndex: [0, 0],
    multiArray: [],
    //一级
    xiaoquArr: [],
    xiaoquList: [],
    //二级
    classArr: [],
    classList: [],
    first: true,
    oldsort: '',
    oldcar: '',
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //点击显示底部弹出
    changeRange: function (e) {
      console.log(e, 4444)
      this.setData({
        goodsname: e.currentTarget.dataset.goodsinfo.goods_name,
        goodssort: e.currentTarget.dataset.goodsinfo.sort,
        oldsort: e.currentTarget.dataset.goodsinfo.sort,
        goodscat_id1: e.currentTarget.dataset.goodsinfo.store_cat_id1,
        oldcar: e.currentTarget.dataset.goodsinfo.store_cat_id1,
        goodscat_id2: e.currentTarget.dataset.goodsinfo.store_cat_id2,
        goodscat_name1: e.currentTarget.dataset.goodsinfo.store_cat_name1,
        goodscat_name2: e.currentTarget.dataset.goodsinfo.store_cat_name2,
        prince: e.currentTarget.dataset.goodsinfo.shop_price,
        goodsweight: e.currentTarget.dataset.goodsinfo.weight,
        allnum: e.currentTarget.dataset.goodsinfo.store_count,
        ids: e.currentTarget.dataset.goods_id,
        className: e.currentTarget.dataset.goodsinfo.store_cat_name1 + '-' + e.currentTarget.dataset.goodsinfo.store_cat_name2
      })
      this.showModal();
      this.showOne();
    },
    // 商品名称
    changeName(e) {
      const that = this;
      that.setData({
        goodsname: e.detail.value
      })
    },

    // 商品序列
    changeSort(e) {
      const that = this;
      that.setData({
        goodssort: e.detail.value
      })
    },

    // 商品价格
    changePrinse(e) {
      const that = this;
      that.setData({
        prince: e.detail.value
      })
    },
    // 商品重量
    changeWeight(e) {
      const that = this;
      that.setData({
        goodsweight: e.detail.value
      })
    },
    // 商品库存
    changeNum(e) {
      const that = this;
      that.setData({
        allnum: e.detail.value
      })
    },


    //确定
    toFillOrder() {
      const that = this;
      var isreload = false;
      var isdell = false;

      that.hideModal();
      if (that.data.oldsort != that.data.goodssort) {
        isreload = true;
      }
      if (that.data.oldcar != that.data.goodscat_id1) {
        isdell = true;
      }

      var obj = {
        'goods_name': that.data.goodsname,
        'goods_weight': that.data.goodsweight,
        'sort': that.data.goodssort,
        'store_cat_id1': that.data.goodscat_id1,
        'store_cat_id2': that.data.goodscat_id2,
        'prince': that.data.prince,
        'nums': that.data.allnum,
        'goods_id': that.data.ids,
        'isreload':isreload ,
        'isdell':isdell ,
      }
      that.triggerEvent('showTab', obj)
    },

    //底部弹出框
    showModal: function () {
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })

      wx.hideTabBar({
        animation: true,
      })

      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: true
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)
    },

    //点击背景面任意一处时，弹出框隐藏
    hideModal: function () {
      //弹出框消失动画
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })

      wx.showTabBar({
        animation: true,
      })

      //this.animation = animation
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
      }.bind(this), 200)
    },

    //商品类目
    showOne() {
      const that = this;
      that.setData({
        hiddenLoading: false
      })
      myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
        token: app.globalData.token,
      }).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.data.status == 1) {
          var xiaoquList = res.data.result;
          var xiaoquArr = xiaoquList.map(item => { // 此方法将校区名称区分到一个新数组中
            return item.cat_name;
          });
          that.setData({
            multiArray: [xiaoquArr, []],
            xiaoquList,
            xiaoquArr
          })

          var index = 0;
          xiaoquList.forEach(item => {
            console.log(item.cat_id, that.data.goodscat_id1, 3333)
            if (item.cat_id == that.data.goodscat_id1) {
              index = xiaoquList.indexOf(item);
            }
          });
          var default_xiaoqu_id = xiaoquList[index]['cat_id']; //获取默认的校区对应的 teach_area_id
          if (default_xiaoqu_id) {
            that.searchClassInfo(default_xiaoqu_id, index) // 如果存在调用获取对应的班级数据
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      })
    },
    searchClassInfo(xiaoqu_id, indexs) {
      var that = this;
      if (xiaoqu_id) {
        myRequest.EasyHttp.post(api.reqUrls.getgetLevel2CatByLevel1Cat, {
          token: app.globalData.token,
          cat_id: xiaoqu_id
        }).then(res => {
          that.setData({
            hiddenLoading: true
          })
          if (res.data.status == 1) {
            var classList = res.data.result;
            var classArr = classList.map(item => {
              return item.cat_name;
            })
            var xiaoquArr = this.data.xiaoquArr;

            if (that.data.first) {
              var index = 0;
              classList.forEach(item => {
                if (item.cat_id == that.data.goodscat_id2) {
                  index = classList.indexOf(item);
                }
              });
              that.setData({
                multiIndex: [indexs, index],
              })
            }

            that.setData({
              multiArray: [xiaoquArr, classArr],
              classArr,
              classList
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        })

      }
    },
    bindMultiPickerColumnChange: function (e) {
      //e.detail.column 改变的数组下标列, e.detail.value 改变对应列的值
      console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
      var data = {
        multiArray: this.data.multiArray,
        multiIndex: this.data.multiIndex
      };
      this.setData({
        first: false
      })
      data.multiIndex[e.detail.column] = e.detail.value;
      var teach_area_id_session = this.data.cat_id; // 保持之前的校区id 与新选择的id 做对比，如果改变则重新请求数据
      switch (e.detail.column) {
        case 0:
          var xiaoquList = this.data.xiaoquList;
          var teach_area_id = xiaoquList[e.detail.value]['cat_id'];
          if (teach_area_id_session != teach_area_id) { // 与之前保持的校区id做对比，如果不一致则重新请求并赋新值
            this.searchClassInfo(teach_area_id);
          }
          data.multiIndex[1] = 0;
          break;
      }
      this.setData(data);
    },
    bindMultiPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      var class_key = 0;
      var xiaoqu_key = 0;
      var xiaoquList = this.data.xiaoquList;
      var classList = this.data.classList;
      var xiaoqu_key = e.detail.value[0];
      var class_key = e.detail.value[1];
      this.setData({
        goodscat_id1: xiaoquList[xiaoqu_key]['cat_id'],
        goodscat_id2: classList.length >0 ? classList[class_key]['cat_id'] : '',
        multiIndex: e.detail.value
      })
    },
  }
})